#!/bin/bash

# MD2PPT 项目结构优化脚本
# 这个脚本将进行非破坏性的结构优化

set -e

echo "🔄 开始优化 MD2PPT 项目结构..."

# 1. 创建文档目录
echo "📁 创建文档目录..."
mkdir -p docs/

# 2. 移动文档文件到 docs 目录
echo "📄 整理文档文件..."
mv API_MCP_DESIGN.md docs/api-mcp-design.md 2>/dev/null || true
mv architecture_proposal.md docs/architecture-proposal.md 2>/dev/null || true
mv DEPLOYMENT.md docs/deployment.md 2>/dev/null || true
mv PROJECT_STRUCTURE_OPTIMIZATION.md docs/project-structure-optimization.md 2>/dev/null || true

# 保留项目原始文档在根目录
cp prd.md docs/product-requirements.md 2>/dev/null || true

# 3. 创建配置目录
echo "⚙️  创建配置目录..."
mkdir -p config/docker config/environments

# 4. 移动 Docker 配置
echo "🐳 整理 Docker 配置..."
mv docker-compose.yml config/docker/ 2>/dev/null || true
mv docker-compose.prod.yml config/docker/ 2>/dev/null || true

# 5. 移动 Nginx 配置
echo "🌐 整理 Nginx 配置..."
mv nginx config/ 2>/dev/null || true

# 6. 移动监控配置
echo "📊 整理监控配置..."
mv monitoring config/ 2>/dev/null || true

# 7. 创建环境配置文件
echo "🔧 创建环境配置文件..."
cp .env.example config/environments/development.env 2>/dev/null || true
cp .env.example config/environments/staging.env 2>/dev/null || true
cp .env.example config/environments/production.env 2>/dev/null || true

# 8. 整理依赖文件
echo "📦 整理依赖文件..."
mkdir -p config/requirements/

mv requirements-api.txt config/requirements/ 2>/dev/null || true
mv requirements-test.txt config/requirements/ 2>/dev/null || true

# 9. 创建简化的 pyproject.toml
echo "⚡ 创建 pyproject.toml..."
cat > pyproject.toml << 'EOF'
[build-system]
requires = ["setuptools>=61.0", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "md2ppt"
version = "1.0.0"
description = "Convert Markdown to PowerPoint presentations with API and MCP support"
readme = "README.md"
license = {text = "MIT"}
authors = [
    {name = "MD2PPT Team", email = "team@md2ppt.dev"}
]
classifiers = [
    "Development Status :: 4 - Beta",
    "Intended Audience :: Developers",
    "License :: OSI Approved :: MIT License",
    "Programming Language :: Python :: 3",
    "Programming Language :: Python :: 3.9",
    "Programming Language :: Python :: 3.10",
    "Programming Language :: Python :: 3.11",
]
requires-python = ">=3.9"

# 核心依赖
dependencies = [
    "python-pptx>=0.6.21",
    "markdown>=3.5.1",
    "Pillow>=10.1.0",
    "imageio>=2.31.5",
    "imageio-ffmpeg>=0.4.9",
    "pydantic>=2.5.0",
]

[project.optional-dependencies]
# API 服务依赖
api = [
    "fastapi>=0.104.1",
    "uvicorn>=0.24.0",
    "pydantic-settings>=2.0.0",
    "aiofiles>=23.2.0",
    "python-multipart>=0.0.6",
]

# MCP 服务器依赖
mcp = [
    "mcp>=1.9.4",
]

# 开发依赖
dev = [
    "pytest>=7.4.3",
    "pytest-asyncio>=0.21.1",
    "pytest-cov>=4.1.0",
    "pytest-mock>=3.12.0",
    "httpx>=0.25.2",
    "black>=23.0.0",
    "ruff>=0.1.0",
    "mypy>=1.0.0",
]

# 完整安装（包含所有功能）
all = [
    "md2ppt[api,mcp,dev]"
]

[project.scripts]
md2ppt = "md2ppt.main:main"

[project.urls]
Homepage = "https://github.com/your-org/md2ppt"
Documentation = "https://github.com/your-org/md2ppt/tree/main/docs"
Repository = "https://github.com/your-org/md2ppt"
"Bug Tracker" = "https://github.com/your-org/md2ppt/issues"

[tool.setuptools.packages.find]
include = ["md2ppt*", "api*", "mcp*", "shared*"]

[tool.black]
line-length = 100
target-version = ['py39']

[tool.ruff]
line-length = 100
target-version = "py39"

[tool.mypy]
python_version = "3.9"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true

[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = ["test_*.py"]
python_classes = ["Test*"]
python_functions = ["test_*"]
addopts = "-v --tb=short --strict-markers"
markers = [
    "slow: marks tests as slow",
    "integration: marks tests as integration tests",
    "unit: marks tests as unit tests",
    "api: marks tests as API tests",
    "mcp: marks tests as MCP tests",
]
asyncio_mode = "auto"
EOF

# 10. 创建新的简洁 README
echo "📚 创建新的 README..."
cat > NEW_README.md << 'EOF'
# MD2PPT - Markdown to PowerPoint Converter

Convert Markdown files to professional PowerPoint presentations with ease.

[![Tests](https://github.com/your-org/md2ppt/workflows/tests/badge.svg)](https://github.com/your-org/md2ppt/actions)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Python 3.9+](https://img.shields.io/badge/python-3.9+-blue.svg)](https://www.python.org/downloads/)

## ✨ Features

- 🎯 **One-click conversion** from Markdown to PowerPoint
- 🎨 **Multiple themes** (default, blue, dark, minimal)
- 📷 **Image support** with automatic sizing
- 🎬 **Video embedding** with smart aspect ratio handling
- 🌐 **REST API** for web integration
- 🔧 **MCP Server** for AI assistant integration
- 🐳 **Docker support** for easy deployment

## 🚀 Quick Start

### Installation

```bash
# Basic installation
pip install md2ppt

# With API support
pip install md2ppt[api]

# With MCP support
pip install md2ppt[mcp]

# Full installation
pip install md2ppt[all]
```

### Basic Usage

```bash
# CLI conversion
md2ppt presentation.md

# With custom theme
md2ppt presentation.md --theme blue -o output.pptx
```

### API Server

```bash
# Start API server
python -m md2ppt --api

# API available at http://localhost:8000
# Documentation at http://localhost:8000/docs
```

### MCP Server

```bash
# Start MCP server for AI assistants
python -m md2ppt --mcp
```

### Docker

```bash
# Quick start with Docker
docker-compose -f config/docker/docker-compose.yml up -d
```

## 📖 Documentation

- [📘 **Installation Guide**](docs/installation.md)
- [🔧 **API Reference**](docs/api-reference.md)
- [🤖 **MCP Integration**](docs/mcp-integration.md)
- [🚀 **Deployment Guide**](docs/deployment.md)
- [🏗️ **Architecture**](docs/architecture.md)

## 🎯 Use Cases

- **Technical Presentations**: Convert technical documentation to slides
- **API Integration**: Embed in web applications
- **AI Assistants**: Integrate with Claude, ChatGPT, etc.
- **Automation**: Batch convert documentation to presentations
- **Team Workflows**: Standardize presentation creation

## 📋 Example

**Input Markdown:**
```markdown
# My Presentation

## Introduction
- Welcome to MD2PPT
- Easy conversion process
- Professional results

## Features
![Architecture](./diagram.png)

### Core Capabilities
- Multiple themes
- Video support
- API integration
```

**Output:** Professional PowerPoint presentation with proper formatting, themes, and media handling.

## 🛠️ Development

```bash
# Clone repository
git clone https://github.com/your-org/md2ppt.git
cd md2ppt

# Install development dependencies
pip install -e .[dev]

# Run tests
pytest

# Start development API server
python -m md2ppt --api
```

## 📊 Project Stats

- **Language**: Python 3.9+
- **Framework**: FastAPI (API), MCP (AI Integration)
- **Testing**: 91% test coverage
- **Architecture**: Modular, extensible design
- **Deployment**: Docker, cloud-ready

## 🤝 Contributing

We welcome contributions! Please see our [Contributing Guide](CONTRIBUTING.md) for details.

## 📄 License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## 🙏 Acknowledgments

- [python-pptx](https://github.com/scanny/python-pptx) for PowerPoint generation
- [FastAPI](https://fastapi.tiangolo.com/) for the API framework
- [MCP](https://modelcontextprotocol.io/) for AI assistant integration
EOF

# 11. 更新脚本路径
echo "🔧 更新脚本配置..."
if [ -f "scripts/deploy.sh" ]; then
    sed -i.bak 's|docker-compose|config/docker/docker-compose|g' scripts/deploy.sh
fi

# 12. 创建简化的 Docker Compose 引用
echo "🐳 创建 Docker Compose 快捷方式..."
cat > docker-compose.yml << 'EOF'
# Quick start Docker Compose
# For production use: config/docker/docker-compose.prod.yml

version: '3.8'
services:
  md2ppt:
    build: .
    ports:
      - "8000:8000"
    volumes:
      - ./storage:/app/storage
    environment:
      - API_HOST=0.0.0.0
      - API_PORT=8000
    restart: unless-stopped
EOF

# 13. 创建开发工具
echo "🛠️  创建开发工具..."
mkdir -p tools/

cat > tools/dev-setup.sh << 'EOF'
#!/bin/bash
# 开发环境设置脚本

echo "🚀 设置 MD2PPT 开发环境..."

# 安装开发依赖
pip install -e .[dev]

# 创建 .env 文件
if [ ! -f ".env" ]; then
    cp config/environments/development.env .env
    echo "✅ 创建了 .env 文件"
fi

# 运行测试
echo "🧪 运行测试..."
pytest

echo "✅ 开发环境设置完成！"
echo ""
echo "可用命令："
echo "  python -m md2ppt --api      # 启动 API 服务器"
echo "  python -m md2ppt --mcp      # 启动 MCP 服务器"
echo "  pytest                      # 运行测试"
echo "  black .                     # 格式化代码"
echo "  ruff check .                # 代码检查"
EOF

chmod +x tools/dev-setup.sh

# 14. 创建 .dockerignore
echo "🐳 创建 .dockerignore..."
cat > .dockerignore << 'EOF'
# Git
.git/
.gitignore

# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
env/
venv/
.venv/
pip-log.txt
pip-delete-this-directory.txt
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.log
.pytest_cache/

# Documentation
docs/
*.md
!README.md

# Development
.env
.env.*
tests/
tools/

# IDE
.vscode/
.idea/
*.swp
*.swo

# OS
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# Temporary files
storage/
logs/
*.tmp
*.temp
EOF

echo ""
echo "✅ 项目结构优化完成！"
echo ""
echo "📁 新的目录结构："
echo "  ├── config/          # 配置文件"
echo "  ├── docs/            # 文档"
echo "  ├── tools/           # 开发工具"
echo "  ├── pyproject.toml   # Python 项目配置"
echo "  └── NEW_README.md    # 新的 README（请手动替换）"
echo ""
echo "🔄 下一步："
echo "  1. 检查新的目录结构是否正确"
echo "  2. 替换 README.md: mv NEW_README.md README.md"
echo "  3. 测试功能: python -m md2ppt --api"
echo "  4. 运行测试: pytest"
echo ""